package Gui;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import controller.DealControl;
import controller.MarketingTemplateControl;


import Client_Server.ClientConsole;
import Entity.Deal;

import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.toedter.calendar.JDateChooser;
import java.awt.SystemColor;


// TODO: Auto-generated Javadoc
/**
 * The Class DealMenu.
 */
public class DealMenu extends JPanel{
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -6400283674228776823L;

	/** The check deal button. */
	public static JButton checkDealButton;
	
	/** The New deal button. */
	public static JButton NewDealButton;
	
	/** The confirmation lable. */
	public static JLabel confirmationLable=null;
	
	/** The confirmation lable2. */
	public static JLabel confirmationLable2=null;
	
	/** The confirmation lable3. */
	public static JLabel confirmationLable3=null;
	
	/** The back button. */
	public static JButton backButton = new JButton("Back");
	
	/** The btn search deal. */
	public static JButton btnSearchDeal = new JButton("Search Deal");
	
	/** The text area. */
	public static JTextArea textArea = null;
	
	/** The lbl new label_5. */
	public static JLabel lblNewLabel_5 = new JLabel("% Discount:");
	
	/** The lbl new label_4. */
	public static JLabel lblNewLabel_4 = new JLabel("Tamplate Id:");
	
	/** The lbl new label_3. */
	public static JLabel lblNewLabel_3 = new JLabel("End Date:");
	
	/** The lbl new label_2. */
	public static JLabel lblNewLabel_2 = new JLabel("Start Date:");
	
	/** The lbl new label_1. */
	public static JLabel lblNewLabel_1 = new JLabel("Deal ID:");
	
	/** The lbl new label. */
	public static JLabel lblNewLabel = new JLabel("Add New Deal");
	
	/** The text area_1. */
	public static JTextArea textArea_1 = new JTextArea();
	
	/**
	 * The text area_2. */
	public static JTextArea textArea_2 = new JTextArea();
	
	/** The dc. */
	public static DealControl dc= new DealControl();
	
	/** The text field. */
	static JTextField textField;
	
	/** The text field_3. */
	static JTextField textField_3;
	
	/** The text field_4. */
	static JTextField textField_4;
	
	/** The btn save the deal. */
	static JButton btnSaveTheDeal;
	/**
	 * The btn display the templates
	 */
	static JButton DispNewButton = new JButton("DISP");
	
	/** The deal. */
	private Deal deal=null;
	
	/** The text field_5. */
	static JTextField textField_5;
	
	/** The del but. */
	static JButton delBut = new JButton("Remove Deal");
	private String s;
	private String s2;
	static final JDateChooser endDate = new JDateChooser();
	static final JDateChooser startDate = new JDateChooser();
;
	
	/**
	 * Instantiates a new deal menu.
	 */
	public DealMenu()
	{
		super();
		viewCreateDeal();
	}

	/**
	 * View create deal.
	 */
	public void viewCreateDeal(){
		MarketingDirectorGui.WelcomeLabel.setVisible(false);
		this.setSize( 1500,765  ); //set Frame size
		this.setLayout(null); //set layout
		this.setBackground(Color.LIGHT_GRAY); // set Background color to white
		
		backButton.setBounds(554, 412, 172, 29);   //set button's size and location
		add(backButton);
		
		startDate.setBackground(SystemColor.inactiveCaption);
		startDate.setBounds(141, 106, 200, 20);
		startDate.setVisible(false);
		add(startDate);
		
		
		endDate.setBackground(SystemColor.inactiveCaption);
		endDate.setBounds(141, 137, 200, 20);
		endDate.setVisible(false);
		add(endDate);
		
		
		startDate.getSpinner().addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				java.sql.Date sqldate = new java.sql.Date(startDate.getDate().getTime());
			    s=sqldate.toString();

			}
		});
		
		endDate.getSpinner().addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				java.sql.Date sqldate = new java.sql.Date(endDate.getDate().getTime());
			    s2=sqldate.toString();

			}
		});
		
		checkDealButton = new JButton("Check Deals"); //create "Display" button
		checkDealButton.setFont(new Font("Courier New", Font.BOLD, 12));
		checkDealButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				DealControl.checkDeals();
				textArea.setVisible(true);
				delBut.setVisible(false);
			}
		});
		checkDealButton.setBounds(554, 55, 172, 29);    //set button size
		add(checkDealButton); //add the button to the frame
		
		NewDealButton = new JButton("Create New Deal"); //create "Display" button
		NewDealButton.setFont(new Font("Courier New", Font.BOLD, 12));
		NewDealButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 textField.setVisible(true);
				 textField_3.setVisible(true);
				 textField_4.setVisible(true);
				 lblNewLabel_5.setVisible(true);
				 lblNewLabel_4.setVisible(true);
				 lblNewLabel_3.setVisible(true);
				 lblNewLabel_2.setVisible(true);
				 lblNewLabel_1.setVisible(true);
				 lblNewLabel.setVisible(true);
				 btnSaveTheDeal.setVisible(true);
				 startDate.setVisible(true);
				 endDate.setVisible(true);
				 textArea_2.setVisible(true);
				 DispNewButton.setVisible(true);
				 }
			});
		NewDealButton.setBounds(554, 80, 172, 29);    //set button size
		add(NewDealButton); //add the button to the frame
			
		
		confirmationLable = new JLabel("\n");   //set label for result
		confirmationLable.setFont(new Font("Courier New", Font.BOLD | Font.ITALIC, 15));
		confirmationLable.setForeground(new Color(0, 0, 128));
		confirmationLable.setBounds(75, 567, 651, 63);  //set label size
		add(confirmationLable);              // add the label to the frame
		
		
		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setFont(new Font("Courier New", Font.BOLD | Font.ITALIC, 15));
		textArea.setForeground(new Color(0, 0, 128));
		textArea.setBackground(Color.LIGHT_GRAY);
		textArea.setBounds(803, 156, 442, 454);
		add(textArea);
		
		textField = new JTextField();
		textField.setBounds(178, 72, 86, 20);
		textField.setVisible(false);
		add(textField);
		textField.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setBounds(178, 170, 86, 20);
		textField_3.setVisible(false);
		add(textField_3);
		textField_3.setColumns(10);
		
		textField_4 = new JTextField();
		textField_4.setBounds(178, 201, 86, 20);
		textField_4.setVisible(false);
		add(textField_4);
		textField_4.setColumns(10);
		
		lblNewLabel.setFont(new Font("Stencil", Font.PLAIN, 14));
		lblNewLabel.setBackground(Color.WHITE);
		lblNewLabel.setToolTipText("______");
		lblNewLabel.setForeground(Color.ORANGE);
		lblNewLabel.setBounds(75, 30, 152, 41);
		lblNewLabel.setVisible(false);
		add(lblNewLabel);
		
		
		lblNewLabel_1.setForeground(Color.BLUE);
		lblNewLabel_1.setBounds(75, 75, 46, 14);
		lblNewLabel_1.setVisible(false);
		add(lblNewLabel_1);
		
		
		lblNewLabel_2.setForeground(Color.BLUE);
		lblNewLabel_2.setBounds(75, 110, 67, 14);
		lblNewLabel_2.setVisible(false);
		add(lblNewLabel_2);
		
		
		lblNewLabel_3.setForeground(Color.BLUE);
		lblNewLabel_3.setBounds(75, 139, 67, 14);
		lblNewLabel_3.setVisible(false);
		add(lblNewLabel_3);
		
		
		lblNewLabel_4.setForeground(Color.BLUE);
		lblNewLabel_4.setBounds(75, 173, 80, 14);
		lblNewLabel_4.setVisible(false);
		add(lblNewLabel_4);
		
		
		lblNewLabel_5.setForeground(Color.BLUE);
		lblNewLabel_5.setBounds(75, 204, 67, 14);
		lblNewLabel_5.setVisible(false);
		add(lblNewLabel_5);
		textArea_1.setFont(new Font("Courier New", Font.BOLD | Font.ITALIC, 15));
		
		textArea_1.setEditable(false);
		textArea_1.setBackground(Color.LIGHT_GRAY);
		textArea_1.setForeground(new Color(0, 0, 128));
		textArea_1.setBounds(75, 486, 651, 51);
		add(textArea_1);
		
		btnSaveTheDeal = new JButton("Save The Deal");
		btnSaveTheDeal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.out.println(textField.getText()+s+s2+textField_3.getText()+textField_4.getText());
				deal=new Deal(textField.getText(),s,s2,textField_3.getText(),textField_4.getText());
				if( s==null  || s2==null){
					confirmationLable.setText("Please Choose Dates !");
					confirmationLable.setVisible(true);
				}
				if(dc.checkFields(deal) ){
					confirmationLable.setText("Please Fill All The Fields !");
					confirmationLable.setVisible(true);
				}
				else {
					confirmationLable.setVisible(false);
					textArea_1.setText("");
					dc.AddDealToDB(deal);
					textArea_1.setVisible(true);
				}
			}});
		btnSaveTheDeal.setBackground(Color.WHITE);
		btnSaveTheDeal.setForeground(new Color(47, 79, 79));
		btnSaveTheDeal.setFont(new Font("Courier New", Font.BOLD, 15));
		btnSaveTheDeal.setBounds(141, 281, 163, 29);
		btnSaveTheDeal.setVisible(false);
		add(btnSaveTheDeal);
		
		textField_5 = new JTextField();
		textField_5.setBounds(803, 89, 86, 20);
		add(textField_5);
		textField_5.setColumns(10);
		
		
		btnSearchDeal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				textArea_1.setVisible(false);
				textArea.setVisible(false);
				textArea.setText("");
				if (textField_5.getText().isEmpty()){
					textArea.setText("Please fill The Search Field!!");
					textArea.setVisible(true);
				}
				else {
					String toSend=textField_5.getText(); 
					dc.SearchDealMenu(toSend);
					textArea.setVisible(true);
				}
				
			}});
		btnSearchDeal.setHorizontalAlignment(SwingConstants.LEFT);
		btnSearchDeal.setFont(new Font("Courier New", Font.BOLD, 13));
		btnSearchDeal.setBounds(803, 55, 127, 23);
		add(btnSearchDeal);
		
		
		delBut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 int reply = JOptionPane.showConfirmDialog
						 (null, "Remove Deal From System ?", "Confirm Action", JOptionPane.YES_NO_OPTION);
				    if (reply == JOptionPane.YES_OPTION)
				    {
				    	textArea.setVisible(false);
						textArea.setText("");
				    	dc.DeleteDeal(textField_5.getText());
				    	textArea.setVisible(true);
				    }
			}});
		delBut.setForeground(Color.RED);
		delBut.setFont(new Font("Courier New", Font.BOLD, 12));
		delBut.setBounds(949, 55, 127, 23);
		delBut.setVisible(false);
		add(delBut);
		
		
		DispNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dc.displayTemplates();
				textArea_2.setVisible(true);
			}});
		DispNewButton.setHorizontalAlignment(SwingConstants.LEADING);
		DispNewButton.setBounds(265, 170, 70, 17);
		DispNewButton.setVisible(false);
		add(DispNewButton);
		
		textArea_2.setForeground(new Color(0, 0, 128));
		textArea_2.setBackground(new Color(192, 192, 192));
		textArea_2.setFont(new Font("Courier New", Font.PLAIN, 14));
		textArea_2.setBounds(381, 169, 162, 181);
		textArea_2.setVisible(false);
		add(textArea_2);
		
		confirmationLable.setVisible(false);
		textArea_1.setVisible(false);
		textArea_2.setVisible(false);
		}
	}
